<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      class="mt-dialog"
      width="80%"
      @close="close"
    >

      <div class="top">
        <el-input
          v-model="kwInput"
          placeholder="输入模块名称进行查询"
          class="input-with-select"
          clearable
          @keyup.native.enter="handleSearch"
        >
          <el-button slot="append" icon="el-icon-search" @click="handleSearch">查询状态</el-button>
        </el-input>
      </div>

      <el-table
        v-loading.fullscreen.lock="listLoading"
        :data="data"
        border
        fit
        highlight-current-row
        style="width: 100%"
        @sort-change="handleSort"
      >
        <el-table-column align="center" label="模块名称" prop="name" sortable="custom" />
        <el-table-column align="center" label="版本" prop="version" sortable="custom" />
        <el-table-column align="center" label="模块ID" prop="id_module" sortable="custom" />
        <el-table-column align="center" label="状态" prop="enable" sortable="custom">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enable"
              active-text="enable"
              inactive-text="disable"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="handleChangeModuleStatus(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { SearchPrestashopModule, SetPrestashopModule } from '@/api/prestashop/module'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Webmodules',
  props: ['showDialog', 'website_id', 'domain'],
  data() {
    return {
      data: [],
      listLoading: true,
      kwInput: 'paypal',
      order_by: '',
      order_desc: '',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,

      action: null,
      dialogVisible: false,
      title: '网站模块管理',
      temp: {
        amount: '',
        ads_id: this.ads_id,
        note: ''
      },
      width: '',
      innerDialogVisible: false
    }
  },
  watch: {
    website_id(v) {
      if (v) {
        this.searchModule()
      }
    },
    domain(v) {
      if (v) {
        this.title = '网站' + v + '模块管理'
      }
    },
    showDialog(v) {
      this.dialogVisible = v
    }
  },
  methods: {
    searchModule() {
      this.listLoading = true
      SearchPrestashopModule({
        kw: this.kwInput,
        website_id: this.website_id
      })
        .then(response => {
          this.scrollTop()
          this.data = response.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    handleChangeModuleStatus(row) {
      SetPrestashopModule({
        id_module: row.id_module,
        enable: row.enable,
        website_id: this.website_id
      })
        .then(response => {
          this.scrollTop()
          this.notify('修改成功')
          this.searchModule()
        }).catch(err => {
          console.log(err)
          this.notify('修改失败')
        })
      console.log('change module status')
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.searchModule()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.searchModule()
    },
    // 点击搜索
    handleSearch() {
      this.searchModule()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.searchModule()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    close() {
      this.temp = {
        amount: '',
        ads_id: '',
        note: ''
      }
      this.$emit('closeMyDialog', false)
    }
  }
}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .top >>> .el-select .el-input {
    width: 350px;
  }

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .top {
    padding-left: 20px;
    margin-bottom: 30px;
    margin-top: 28px;
  }

  .input-with-select {
    width: calc(100% - 200px);
  }

  .add-btn {
    float: right;
    margin-right: 20px;
    width: 150px
  }

  .mt-pagination {
    float: right;
    margin-top: 5px;
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 68px;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin-top: 60px;
    /*margin: 168px auto !important;*/
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 100px;
  }
</style>
